package com.zhika.zishi.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageButton;
import android.widget.TableRow;
import android.widget.TextView;
import android.widget.Toast;

import com.zhika.zishi.R;
import com.zhika.zishi.activity.EditStrategyActivity;
import com.zhika.zishi.activity.SuperaddMyselfProblemActivity;
import com.zhika.zishi.beans.MySelfSPBean;

import java.util.ArrayList;

/**
 * Created by shi-02 on 2015/4/14.
 * 个人主页上的问题和攻略的适配器
 */
public class MySelfSPAdapter extends BaseAdapter {


    private Context context;

    private ArrayList<MySelfSPBean> mySelfSPList;

    public MySelfSPAdapter(Context context,ArrayList<MySelfSPBean> mySelfSPList){
        this.context = context;

        this.mySelfSPList = mySelfSPList;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public int getCount() {
        return mySelfSPList.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view;
        ViewHolder viewHolder;
        if (convertView == null){
            view = LayoutInflater.from(context).inflate(R.layout.myself_strategy_problem_layout,null);
            viewHolder = new ViewHolder();
            viewHolder.titleNameText = (TextView) view.findViewById(R.id.myself_strategy_problem_title_text);
            viewHolder.commentNumText = (TextView) view.findViewById(R.id.myself_strategy_problem_comment_text);
            viewHolder.collectNumText = (TextView) view.findViewById(R.id.myself_strategy_problem_collect_text);
            viewHolder.likeNumText = (TextView) view.findViewById(R.id.myself_strategy_problem_like_text);
            viewHolder.likeRow = (TableRow) view.findViewById(R.id.myself_strategy_problem_like_row);
            viewHolder.editImageButton = (ImageButton) view.findViewById(R.id.myself_strategy_problem_edit_image_button);
            view.setTag(viewHolder);
        }else {
            view = convertView;
            viewHolder = (ViewHolder) view.getTag();
            resetViewHolder(viewHolder);
        }
        MySelfSPBean bean = mySelfSPList.get(position);

        if (bean.getTaInfoStr()!=null && bean.getTaInfoStr().equals("ta")){
            viewHolder.editImageButton.setVisibility(View.GONE);
        }

        final String type = bean.getType();
        if (type.equals("problem")){
             viewHolder.likeRow.setVisibility(View.GONE);
        }else {
            viewHolder.likeRow.setVisibility(View.VISIBLE);
            viewHolder.likeNumText.setText(bean.getLikeNumStr());
        }

        viewHolder.titleNameText.setText(bean.getTitleNameStr());

        viewHolder.commentNumText.setText(bean.getCommentNumStr());

        viewHolder.collectNumText.setText(bean.getCollectNumStr());

        viewHolder.editImageButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (type.equals("strategy")){
                    Intent intent = new Intent(context, EditStrategyActivity.class);
                    context.startActivity(intent);
                }else {
                    Intent intent = new Intent(context, SuperaddMyselfProblemActivity.class);
                    context.startActivity(intent);
                }
            }
        });

        return view;

    }

    private void resetViewHolder(ViewHolder holder){
        holder.titleNameText.setText(null);
        holder.commentNumText.setText(null);
        holder.collectNumText.setText(null);
        holder.likeNumText.setText(null);
    }

    private class ViewHolder{
        private TextView titleNameText;

        private TextView commentNumText;

        private TextView collectNumText;

        private TextView likeNumText;

        private ImageButton editImageButton;

        private TableRow likeRow;

    }
}
